package org.apache.uima.tutorial.ex1;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.jcas.JCas;
import org.apache.uima.tutorial.Ratings;

public class RatingNumber extends JCasAnnotator_ImplBase {
  // create regular expression pattern for Yorktown room number
  private Pattern mYorktownPattern = 
        Pattern.compile("ProductRatingNumerator: [0-9]");

  // create regular expression pattern for Hawthorne room number
  private Pattern RatingDenom = 
      Pattern.compile("ProductRatingDenominator: [0-9]");
  private Pattern PublishedEpoch = 
      Pattern.compile("PublishedEpoch: (19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])");
  private Pattern ExtractedEpoch = 
      Pattern.compile("ExtractedEpoch: (19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])");
  private Pattern emailAd = 
      Pattern.compile("([A-Z])+([a-zA-Z])$");
  
  private Pattern wordsmatcher = 
      Pattern.compile("\\bI\\W+(?:\\w+\\W+){1,6}?love\\b");
 
  private Pattern wordsmatcher1 = 
      Pattern.compile("\\bI\\W+(?:\\w+\\W+){1,6}?like\\b");
  public void process(JCas aJCas) {
    
    // The JCas object is the data object inside UIMA where all the 
    // information is stored. It contains all annotations created by 
    // previous annotators, and the document text to be analyzed.
    
    // get document text from JCas
    String docText = aJCas.getDocumentText();
 
    // search for Yorktown room numbers
    Matcher matcher = mYorktownPattern.matcher(docText);
    int pos = 0;
    while (matcher.find(pos)) {   System.out.println(docText);
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      
    //  System.out.println(matcher.start() + " " + matcher.end());
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
 
      annotation.addToIndexes();
      pos = matcher.end();
    }
  
    matcher = RatingDenom.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
    matcher = PublishedEpoch.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
    // search for Hawthorne room 
    
    matcher = ExtractedEpoch.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
    matcher = emailAd.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
    
    matcher = wordsmatcher.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
    matcher = wordsmatcher1.matcher(docText);
    pos = 0;
    while (matcher.find(pos)) {
      // match found - create the match as annotation in 
      // the JCas with some additional meta information
      Ratings annotation = new Ratings(aJCas);
      annotation.setBegin(matcher.start());
      annotation.setEnd(matcher.end());
    //  annotation.setBuilding("Hawthorne");
      annotation.addToIndexes();
      pos = matcher.end();
    }
  }
}